<?php
//handle post events.
if(isset($_POST['login_submit'])) {
	$email = $_POST['email'];
	$password = makePassword($_POST['password']);
	$query = mysql_query("SELECT * FROM `instructor` WHERE `email`='$email' AND `password`='$password' AND `permission` = '2'") or die(mysql_error());
	$result = mysql_fetch_object($query);
	if($result) {
		$_SESSION['logged_in'] = $result->id;
		setcookie('etat_logged_in', $result->id, time()+COOKIE_TIME);
		unset($_SESSION['m']);
		redirect("myetat.php");
	} else {
		$_SESSION['m'] = 2;
		redirect("index.php");
	}
}
if(isset($_POST['forgot_pass_submit'])) {
	$email = $_POST['email'];
	$query = mysql_query("SELECT * FROM `instructor` WHERE `email`='$email'") or die(mysql_error());
	$result = mysql_fetch_object($query);
	if($result) {
		//@TODO: reset password and send email
		redirect('index.php?m=4');
	} else {
		redirect('index.php?m=6');
	}
}
if (isset($_POST['logout_submit'])) {
	logout();
}
if(isset($_GET['m']) || isset($_SESSION['m'])) {
	if(isset($_GET['m'])) {
		$_SESSION['m'] = $_GET['m'];
	}
	$m = $_SESSION['m'];
	unset($_SESSION['m']);
	switch($m) {
		case '1': $msg = "Logged out successfully"; break;
		case '2': $msg = "Invalid Username/Password, Please try again"; break;
		case '3': $msg = "You need to be logged in to view that page"; break;
		case '4': $msg = "Password reset successful, please check your email"; break;
		case '5': $msg = "Sorry, you cannot access that file."; break;
		case '6': $msg = "Sorry, that Email Address was not found in our database. Please contact the Administrator"; break;
		case '7':
			$sql_query = mysql_query("SELECT * FROM `instructor` WHERE `permission` = '3'") or die(mysql_error());
			$result = mysql_fetch_object($sql_query);
			if($result) {
				$msg = "Sorry, you need to be invited first in order to register. Please <a href='mailto:$result->email'>contact the Administrator</a>.";
			} else {
				$msg = "Sorry, you need to be invited first in order to register. Please contact the Administrator.";
			}
			break;
	}
	unset($_SESSION['m']);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php print $page_title; ?></title>
<?php
foreach($java_script as $js) {
	if(file_exists(ROOT . BRANCH . "/include/js/$js.js"))
	print "<script language='JavaScript' type='text/javascript' src='/include/js/$js.js'></script>";
}
foreach($style_sheet as $css) {
	if(file_exists(ROOT . BRANCH . "/include/css/$css.css"))
	print "<link href='/include/css/$css.css' rel='stylesheet' type='text/css'>";
}
?>
</head>
<body>
<!-- Main Wrapper -->
<div id="fade" class='invisible' onclick='closePopup();'></div>
<div id="main_wrapper">
<div id="top_1">
<div id='loggedin-form' class="right"><?php
if(isLoggedIn()) {
	$form = new Form('logout_form','post');
	$form->text("Welcome, <b>".$instructor->full_name.'</b> ');
	$form->button('logout_submit','Logout',array(),false);
	print $form->build();
}
?></div>
</div>
<div class="title_holder">
<div class="logo_holder"></div>
<div class="titletxt_holder"><img src="include/images/img_tittle.png"
	width="91" height="31" /></div>
<div class="titleright_holder"><img src="include/images/img_etat.png"
	width="390" height="19" /></div>
</div>
<!-- Top part ends --> <!-- Menu -->
<div class="menu_holder">
<div class="menu_cor1"></div>
<!-- Top part --> <?php if(!isLoggedIn()) { ?>
<div id="nav">
<div class="nav-text">eTAT : Web based Teamwork Assessment Tool, CS Dept @ SFSU</div>
</div>
<?php
} else { ?>
<div id="nav">
<div class="chromestyle" id="chromemenu">
<ul>
	<li><a href="index.php">Home</a></li>
	<li><a href="#" rel="dropmenu1">Annotation</a></li>
	<li><a href="#" rel="dropmenu2">Report</a></li>
	<li><a href="#">Survey</a></li>
	<li><a href="#">Documentation</a></li>
</ul>
</div>
</div>
<?php } ?>
<div class="menu_cor2"></div>
</div>
<!-- Menu Ends -->